﻿namespace OasisCiq.Xml
{
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using CommonTypes;
    using External;
    using XS;

    public class CommonTypesXmlReader
    {
        private const string XmlNS = XmlNamespaces.CommonTypes;

        public IEnumerable<_XmlAttribute> ReadAbbreviationGroup(
            IAbbreviationGroup element,
            IEnumerable<_XmlAttribute> attributes)
        {
            foreach (var xmlAttribute in attributes)
            {
                if (xmlAttribute.NS == XmlNS)
                {
                    switch (xmlAttribute.Name)
                    {
                        case "Abbreviation":
                            element.Abbreviation = new XSBoolean(xmlAttribute.Value);
                            continue;
                    }
                }

                // as we are in this point, we don't processed this attribute 
                // and we don't filter it
                yield return xmlAttribute;
            }
        }

        public IEnumerable<_XmlAttribute> ReadDataQualityGroup(
            IDataQualityGroup element,
            IEnumerable<_XmlAttribute> attributes)
        {
            foreach (var xmlAttribute in attributes)
            {
                if (xmlAttribute.NS == XmlNS)
                {
                    switch (xmlAttribute.Name)
                    {
                        case "DataQualityType":
                            element.DataQualityType = new DataQualityTypeList(xmlAttribute.Value);
                            continue;
                        case "ValidFrom":
                            element.ValidFrom = new XSDateTime(xmlAttribute.Value);
                            continue;
                        case "ValidTo":
                            element.ValidTo = new XSDateTime(xmlAttribute.Value);
                            continue;
                    }
                }

                yield return xmlAttribute;
            }
        }

        public IEnumerable<_XmlAttribute> ReadLanguageCodeGroup(
            ILanguageCodeGroup element,
            IEnumerable<_XmlAttribute> attributes)
        {
            foreach (var xmlAttribute in attributes)
            {
                if (xmlAttribute.NS == XmlNS)
                {
                    switch (xmlAttribute.Name)
                    {
                        case "LanguageCode":
                            element.LanguageCode = new XSLanguage(xmlAttribute.Value);
                            continue;
                    }
                }

                yield return xmlAttribute;
            }
        }

        public IEnumerable<_XmlAttribute> ReadValidityDateGroup(
            IValidityDateGroup element,
            IEnumerable<_XmlAttribute> attributes)
        {
            foreach (var xmlAttribute in attributes)
            {
                if (xmlAttribute.NS == XmlNS)
                {
                    switch (xmlAttribute.Name)
                    {
                        case "DateValidFrom":
                            element.DateValidFrom = new XSDateTime(xmlAttribute.Value);
                            continue;
                        case "DateValidTo":
                            element.DateValidTo = new XSDateTime(xmlAttribute.Value);
                            continue;
                    }
                }

                yield return xmlAttribute;
            }
        }

        public void ReadExtendedAttributes(
            IExtendedAttributes element, 
            IEnumerable<_XmlAttribute> attributes)
        {
            if (element.ExtendedAttributes == null)
            {
                element.ExtendedAttributes = new NameValueCollection();
            }

            foreach (var xmlAttribute in attributes)
            {
                var name = xmlAttribute.Name;

                if (!string.IsNullOrEmpty(xmlAttribute.NS))
                {
                    var ns = "{" + xmlAttribute.NS + "}";
                    name = ns + name;
                }
                
                element.ExtendedAttributes.Add(name, xmlAttribute.Value);
            }
        }
    }
}